package com.xmhmyh.generalframework.project.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.constants.Constants;
import com.xmhmyh.generalframework.common.conversion.Bean2SelectConverter;
import com.xmhmyh.generalframework.common.entity.Condition;
import com.xmhmyh.generalframework.common.entity.DataGridRequest;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.*;
import com.xmhmyh.generalframework.common.utils.MyListUtils;
import com.xmhmyh.generalframework.common.vo.SelectVO;
import com.xmhmyh.generalframework.framework.aspectj.annotation.Log;

import com.xmhmyh.generalframework.framework.exceptions.ManageExceptions;
import com.xmhmyh.generalframework.framework.exceptions.WxExceptions;
import com.xmhmyh.generalframework.project.sys.dto.DictionaryDTO;
import com.xmhmyh.generalframework.project.sys.entity.Dictionary;
import com.xmhmyh.generalframework.project.sys.entity.Params;
import com.xmhmyh.generalframework.project.sys.entity.User;
import com.xmhmyh.generalframework.project.sys.enums.DictionaryLvl;
import com.xmhmyh.generalframework.project.sys.enums.ParamsType;
import com.xmhmyh.generalframework.project.sys.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典 Controller
 *
 * @author 于丁
 * @date 2020-08-11 14:49:02
 */
@Api(tags = "数据字典相关接口")
@RestController
@RequestMapping("/sys/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    private IDictionaryService dictionaryService;

    @Log(busType = LogBusType.EDITANDSAVE, source = LogSourceType.MANAGE, module = LogModule.PARAMS)
    @ApiOperation(value = "保存数据字典接口")
    @PreAuthorize("hasAnyAuthority('sys:dictionary:save')")
    @PostMapping("save")
    public Response saveHandler(@RequestBody DictionaryDTO dictionaryDTO) {
        dictionaryService.saveHandler(getLoginUser().getUser(), dictionaryDTO);
        return Response.success();
    }

    @Log(busType = LogBusType.DELETE, source = LogSourceType.MANAGE, module = LogModule.PARAMS)
    @PreAuthorize("hasAuthority('sys:dictionary:del')")
    @ApiOperation(value = "删除数据字典")
    @PostMapping("del")
    public Response delHandler(Long id) {
        Dictionary dictionary = dictionaryService.getById(id);
        if (dictionary == null) {
            throw new ManageExceptions(ResponseCode.ENTITY_NOT_FIND);
        }
        if (dictionary.getType() == ParamsType.OPT.getValue()) {
            if (!getLoginUser().getUser().getAccountId().equals(Constants.SUPER_ACCOUNT_ID)) {
                throw new ManageExceptions(ResponseCode.OPT_DICTIONARY_NOT_REMOVE);
            }
        }
        if (dictionary.getType() == ParamsType.OPT.getValue()) {
            dictionaryService.removeByCode(dictionary.getCode(), null);
        } else {
            dictionaryService.removeById(dictionary.getId());
        }
        return Response.success();
    }

    @PreAuthorize("hasAuthority('sys:dictionary:list')")
    @ApiOperation(value = "查找数据字典列表")
    @PostMapping("pagination")
    public Response paginationHandler(@RequestBody DataGridRequest params) {
        // 如果需要账户约束打开下面注解
        params = addAccountConstraints(params);
        // 只查询lv1的
        params.getConditions().add(new Condition(
                "lvl", Operator.EQ.getLabel(), MyListUtils.genList(String.valueOf(DictionaryLvl.LV1.getValue()))));
        IPage<Dictionary> dictionaryIPage = dictionaryService.paginate(params);
        return Response.success(dictionaryIPage);
    }


    @PreAuthorize("hasAuthority('sys:dictionary:edit')")
    @ApiOperation(value = "获取数据字典的详细数据")
    @PostMapping("loadMore")
    public Response loadMoreHandler(@RequestParam Long id) {
        DictionaryDTO dictionaryDTO = dictionaryService.loadMore(id);
        return Response.success(dictionaryDTO);
    }


    @PostMapping("getSelectVo")
    public Response getSelectVoHandler(String code) {
        User user = getLoginUser().getUser();
        List<SelectVO> selectVOList = dictionaryService.getSelectVoByCode(code, user.getAccountId());
        return Response.success(selectVOList);
    }
}
